﻿using System;
using System.Collections.Generic;

namespace SasPlugIn.DB
{
    public class DbColumnList 
    {
        private List<DbColumn> columns;

        #region Properties

        /// <summary>Interface to Columns</summary>
        public IList<DbColumn> Columns { get { return this.columns.AsReadOnly(); } }

        public int Count { get { return columns.Count; } }

        #endregion

        #region Constructor

        public DbColumnList()
        {
            columns = new List<DbColumn>();
        }
        #endregion

        #region Operator overloading

        // overloads the square brackets to provide array-like access. 
        public DbColumn this[string name]
        {
            get { return this.columns.Find(x => x.Name.ToUpper() == name.ToUpper()); }
        }
        #endregion

        public int Index(string name)
        {
            int index = -1;
            DbColumn c = this[name];
            if (c != null)
            {
                index = columns.IndexOf(c);

            }
            return index;
        }

        #region Column adding

        public void Add(DbColumn column)
        {
            // Todo check if name already exists
            if (this[column.Name] != null)
                throw new ApplicationException("Duplicated column: " + column.Name);

            columns.Add(column);
        }

        #endregion

    }
}
